package com.itrip.service.impl;

import com.itrip.dao.ItripHotelOrderMapper;
import com.itrip.dao.ItripHotelTempStoreMapper;
import com.itrip.dao.ex.OrderMapper;
import com.itrip.dao.ex.RoomMapper;
import com.itrip.dao.ex.StoreMapper;
import com.itrip.dto.RoomDTO;
import com.itrip.pojo.ItripHotelOrder;
import com.itrip.pojo.ItripHotelTempStore;
import com.itrip.service.OrderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {



    @Resource
    private StoreMapper storeMapper;

    @Resource
    private ItripHotelOrderMapper itripHotelOrderMapper;

    @Resource
    private ItripHotelTempStoreMapper itripHotelTempStoreMapper;

    @Resource
    private OrderMapper orderMapper;


    @Override
    public int addOrder(ItripHotelOrder itripHotelOrder) {
        //添加订单
        int orderCount = itripHotelOrderMapper.insertSelective(itripHotelOrder);
        //减少库存
        //创建一个store对象

        int hotelId = Integer.parseInt(itripHotelOrder.getHotelId().toString());
        int roomId = Integer.parseInt(itripHotelOrder.getRoomId().toString());

        ItripHotelTempStore itripHotelTempStore = new ItripHotelTempStore();
        itripHotelTempStore.setHotelId(hotelId);
        itripHotelTempStore.setRoomId(itripHotelOrder.getRoomId());
        itripHotelTempStore.setRecordDate(itripHotelOrder.getCreationDate());
        itripHotelTempStore.setCreationDate(itripHotelOrder.getCreationDate());
        //获取最新的这个酒店房间的数量
        int roomStoreNum = storeMapper.getRoomStoreNum(hotelId,roomId);
        itripHotelTempStore.setStore(roomStoreNum-itripHotelOrder.getCount());
        //插入最新库存记录
        int storeCount = itripHotelTempStoreMapper.insertSelective(itripHotelTempStore);
        return 0;
    }

    @Override
    public int checkUnpayOrder(int userId) {
        return orderMapper.checkUnpayOrder(userId);
    }

    @Override
    public ItripHotelOrder getOrderInfo(Long id) {
        return orderMapper.getOrderInfo(id);
    }

    @Override
    public int setOrderStatus(String orderNo) {
        return orderMapper.setOrderStatus(orderNo);
    }

}
